<?php
/**
 * admin service file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/08/23
 * Time: 15:42
 */
 
namespace Modules\Admin\Service;

use HiCommon\Helper;
use HiCommon\Repository\UsersRepository;
use HiCommon\Repository\UsersIntegralRepository;
use HiCommon\Repository\UsersLevelRepository;
use HiCommon\Repository\OrderMallRepository;
use HiCommon\Repository\OrderRepairstoreRepository;
use HiCommon\Repository\OrderCheckpointRepository;
use HiCommon\Repository\CouponGrantDetailRepository;
use HiCommon\Repository\UsersVehicleRepository;

class UserService
{	

	
	/**
	 *	会员列表
	 *	hinq 2019 08 27
	 *
	 **/
	public function get_list( array $arr_search){
		//获取VIP会员等级
		$list = UsersLevelRepository::get_all();
			
		$sex_types = ['1'=>'男','2'=>'女','3'=>'保密'];
		$arr_list = UsersRepository::get_list( (array) $arr_search );
		$arr_list->appends($arr_search);//添加链接参数
		$arr_list = list_page_data( $arr_list , $arr_search );
		foreach ($arr_list['data'] as $key => &$value) {
			if( !empty( $value['birthday']) ) {
				$value['birthday_msg'] = date("m-d",$value['birthday']);
				$value['sex_msg'] = $sex_types[$value['sex']];
			}
			$value['level_name'] = '极乐';
			foreach ($list as $k => $val) {
				if( $value['integral_all'] >= $val['min_integral'] && $value['integral_all'] <= $val['max_integral'] ){
					$value['level_name'] = $val['name'];
				}
			}
		}
		return hello_success('成功',$arr_list);
	}


	/**
	 *	启用、禁用会员
	 *	hinq 2019 08 27
	 *
	 **/
	public function edit_one( array $arrInput ){
		try {
			//编辑
			unset( $arrInput['_token'] );
            $res = UsersRepository::edit_one( (array) $arrInput);
            if (empty($res)) {
                return hello_error('编辑失败');
            }
            return hello_success('编辑成功', $res);
		} catch (\Exception $exception) {
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
	} 


	/**
	 *	获取积分明细列表
	 *	hinq 2019 08 28
	 *
	 **/

	public function get_list_integral( array $arr_search ){
		$types = ['1'=>'增加积分' , '2'=>'扣除积分'];
		$where['with'] = ['getUser'];
        $arr_list = UsersIntegralRepository::get_list( (array) $arr_search , (array) $where );
        $arr_list->appends($arr_search);//添加链接参数
        $arr_list = list_page_data( $arr_list , $arr_search );

		foreach ($arr_list['data'] as $key => &$value) {
			$value['type_msg'] = $types[$value['type']];
		}

        $arr_add = UsersIntegralRepository::increase_sum( (array) $arr_search , (int) 1 );
        $arr_jian = UsersIntegralRepository::increase_sum( (array) $arr_search , (int) 2 );
        $arr_list['sums'] =[
            'add' => $arr_add,
            'jian' => $arr_jian
        ];
		return hello_success('成功',$arr_list);
	}


	/**
	 *	会员详情获取积分列表【】	
	 *	hinq 2019 10 17
	 *
	 **/
	public function get_detail_list( int $user_id ){
		$types = ['1'=>'增加积分' , '2'=>'扣除积分'];
		$where['with'] = ['getUser'];
		$where['where'] = ['user_id'=>$user_id];
		$where['n_per_page'] = 10;

        $arr_list = UsersIntegralRepository::get_list( (array) $arr_search , (array) $where )->toArray();
        foreach ($arr_list['data'] as $key => &$value) {
			$value['type_msg'] = $types[$value['type']];
			$value['nickname'] = $value['get_user']['nickname'];
			unset($value['get_user']);
		}
       	$temp = [
       		"code"=> 0,
			"msg"=> "",
			"count"=> $arr_list['total'],
			"data"=>$arr_list['data']
       	];
		return $temp;
	}


	/**
	 *	获取会员积分订单明细
	 *	hinq 2019 10 17
	 *
	 **/
	public function get_integral_order_list( int $user_id ){
		$arr_option['where'] = ['user_id'=>$user_id,'order_type'=>2];
        $arr_option['with'] = ['getGoodsInfo', 'getAddressInfo'];
        $arr_option['field'] = ['id','order_no','goods_id','status','pay_status','order_integral','pay_type','pay_time','created_at','user_address_id'];
        $arr_option['n_per_page'] = 10;
        $arr_status = ['0'=>'进行中','1'=>'取消交易','2'=>'待评价','3'=>'已评价','4'=>'已完成','5'=>'异常订单'];
        $arr_pay_status = ['0'=>'待支付','1'=>'已支付'];
		$arr_list = OrderMallRepository::get_list( (array) $arr_option )->toArray();
		foreach ($arr_list['data'] as $key => &$value) {
			$value['goods_name'] = $value['get_goods_info']['name'];
			$value['goods_img'] = $value['get_goods_info']['pic_url']['full_src'];
			unset( $value['get_goods_info'] );
			$value['address_name'] = $value['get_address_info']['username'];
			$value['address_phone'] = $value['get_address_info']['phone'];
			$value['address_info'] = $value['get_address_info']['province'].$value['get_address_info']['city'].$value['get_address_info']['area'].$value['get_address_info']['address'];
			unset(  $value['get_address_info'] );
			$value['status_msg'] = $arr_status[$value['status']];
			$value['pay_status_msg'] = $arr_pay_status[$value['pay_status']];
		}
		$temp = [
       		"code"=> 0,
			"msg"=> "",
			"count"=> $arr_list['total'],
			"data"=>$arr_list['data']
       	];
		return $temp;
	}
	
	/**
	 *	获取会员的服务订单明细
	 *	hinq 2019 10 18
	 *
	 **/
	public function get_repairstore_order_list( int $user_id ){

		$arr_option['where'] = ['user_id'=>$user_id];
		$arr_option['field'] = ['id','order_number','goods_name','goods_num','status','order_price','pay_price','coupon_price','refund_price','refund_cause','created_at','pay_status'];
        // $arr_option['with'] = ['getGoodsInfo', 'getAddressInfo'];
        $arr_option['n_per_page'] = 10;

        $arr_search['arr_pay_status'] = ['1','2','3'];
		$arr_list = OrderRepairstoreRepository::get_list( (array) $arr_option , (array) $arr_search )->toArray();
		$arr_status = ['0'=>'待检修','1'=>'已检修', '2'=>'取消订单' ,'3'=>'已评价','4'=>'已完成'];
		$arr_pay_status = ['0'=>'待支付','1'=>'已支付', '2'=>'申请退款' ,'3'=>'已退款'];
		foreach ($arr_list['data'] as $key => &$value) {
			$value['status'] = $arr_status[$value['status']];
			$value['pay_status'] = $arr_pay_status[$value['pay_status']];
		}
		$temp = [
       		"code"=> 0,
			"msg"=> "",
			"count"=> $arr_list['total'],
			"data"=>$arr_list['data']
       	];
       	return $temp;
	}




	/**
	 *	获取会员预约订单明细
	 *	hinq 2019 10 18
	 *
	 **/
	public function get_checkpoint_order_list( int $user_id ){
		$arr_option['where'] = ['user_id'=>$user_id,'pay_state'=>2];
		$arr_option['field'] = ['*'];
		$arr_option['with'] = ['check_vehicle'];
        $arr_option['n_per_page'] = 10;

		$arr_list = OrderCheckpointRepository::get_list( (array) $arr_option )->toArray();

		foreach ($arr_list['data'] as $key => &$value) {
			$value['pay_state'] = $value['extend']['pay_state']['name'];
			$value['appointment_state'] = $value['extend']['appointment_state']['name'];
			$value['order_state'] = $value['extend']['order_state']['name'];
			unset( $value['extend'] );
			$value['price'] = $value['check_vehicle'][0]['price'];
			$value['refund_price'] = $value['check_vehicle'][0]['refund_price'];
			$value['refund_remarks'] = $value['check_vehicle'][0]['refund_remarks'];
			$value['plate_no'] = $value['check_vehicle'][0]['plate_no'];
			unset( $value['check_vehicle'] );
		}
		$temp = [
       		"code"=> 0,
			"msg"=> "",
			"count"=> $arr_list['total'],
			"data"=>$arr_list['data']
       	];
       	return $temp;
	}


	/**
	 *	获取优惠券列表数据
	 *	hinq 2019 10 18
	 *
	 **/
	public function get_user_coupon( int $user_id ){

		//获取会员的优惠券列表
			$where_coupon = ['user_id'=>$user_id];
			$arr_input['with'] = ['getCouponInfo'];
			$arr_input['n_per_page'] = 10;
			$coupon_list = CouponGrantDetailRepository::get_list_by_where( (array)$where_coupon , (array) $arr_input )->toArray();
			$coupon_pids = ['1'=>'洗车券','2'=>'满减券','3'=>'折扣券'];
			$coupon_status = ['1'=>'未使用','2'=>'已使用','3'=>'已过期'];

			foreach ($coupon_list['data'] as $key => &$value) {
				$value['pid_msg'] = $coupon_pids[$value['pid']];
				$value['name'] = $value['get_coupon_info']['name'];
				$value['content'] = $value['get_coupon_info']['content'];
				$value['coupon_msg'] = '';
				if( $value['get_coupon_info']['pid'] == 2 ){
					$value['coupon_msg'] = '满'.round($value['get_coupon_info']['price_all'] , 2).'减'.round($value['get_coupon_info']['price'],2);
				}else if( $value['get_coupon_info']['pid'] == 3 ){
					$value['coupon_msg'] = '满'.round($value['get_coupon_info']['price_all'] , 2).'打'.round($value['get_coupon_info']['discount_num'] , 2).'折';
				}
				unset($value['get_coupon_info']);
				$value['over_at'] = date('Y-m-d H:i:s',$value['over_at']);
				$value['status'] =  $coupon_status[$value['status']];
			}
			$temp = [
				"code"=> 0,
				"msg"=> "",
				"count"=> $coupon_list['total'],
				"data"=>$coupon_list['data']
            ];
            return $temp;

	}


	/**
     *  获取用户的相关信息
     *  hinq 2019 10 17
     *
     **/
    public function get_user_detail( int $user_id ){
        try{
        	//获取VIP会员等级
			$list = UsersLevelRepository::get_all();
            //获取用户的基本信息
            $user_info =  UsersRepository::get_one( (int) $user_id )->toArray();  
            foreach ($list as $k => $val) {
				if( $user_info['integral_all'] >= $val['min_integral'] && $user_info['integral_all'] <= $val['max_integral'] ){
					$user_info['level_name'] = $val['name'];
				}
			}
			//统计当前用户的已经消费的积分
			$arr_where = ['user_id'=>$user_id,'type'=>2];
			$use_integral = UsersIntegralRepository::get_sum_by_where( (array) $arr_where , (string) 'deduct' );
			$user_info['use_integral'] = $use_integral;
            $data['user_info'] = $user_info;
            $data['user_info']['birthday'] = date('Y-m-d',$user_info['birthday']);
			return hello_success('成功',$data);
        } catch (\Exception $exception) {
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }


    /**
     *  获取指定用户的车辆信息
     *  hinq 2019 12 07
     *
     **/
    public function user_vehicle_list( int $user_id ){
        try{
            if( empty( $user_id ) ){
                return hello_error('参数错误');
            }
            $arr_option['where'] = ['user_id'=>$user_id , 'status'=>1];
            $arr_option['field'] = ['user_id','id','plate_no','car_img','driving_permit_img','vehicle_model_id'];
            $arr_option['with'] = ['simple_vehicle_model'];
            $arr_list = UsersVehicleRepository::get_list( (array) $arr_option )->toArray();
            foreach ( $arr_list['data'] as $key => &$vo ){
                $vo['pinpai'] = $vo['simple_vehicle_model']['name'];
                $vo['name'] = $vo['simple_vehicle_model']['one_name'] .' ' . $vo['simple_vehicle_model']['three_name'];
                $vo['plate_no'] = empty($vo['plate_no']) ? '未绑定' : $vo['plate_no'];
                unset( $vo['simple_vehicle_model'] );
            }
            $temp = [
                "code"=> 0,
                "msg"=> "",
                "count"=> $arr_list['total'],
                "data"=>$arr_list['data'],
            ];
            return $temp;
        }catch (\Exception $exception){
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }


}